package com.qianfeng.zw.meilishuo.fragment;

import android.widget.GridView;
import android.widget.Toast;

import com.alibaba.fastjson.TypeReference;
import com.jayqqaa12.abase.core.fragment.AFragment;
import com.lidroid.xutils.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.PauseOnScrollListener;
import com.qianfeng.zw.meilishuo.R;
import com.qianfeng.zw.meilishuo.adapter.HotSellAdapter;
import com.qianfeng.zw.meilishuo.bean.AppConn;
import com.qianfeng.zw.meilishuo.bean.TwitterGoods;
import com.qianfeng.zw.meilishuo.config.AppConstants;
import com.qianfeng.zw.meilishuo.listener.OnCompleteLinstener;
import com.qianfeng.zw.meilishuo.widget.SwipeRefreshLayout;

import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * 热销
 */
@EFragment(R.layout.fragment_hotsell)
public class HotSellFragment extends AFragment implements SwipeRefreshLayout.OnLoadListener, SwipeRefreshLayout.OnRefreshListener, OnCompleteLinstener {
    @ViewById(R.id.hotsell_stagger)
    GridView staggeredGridView;
    @ViewById(R.id.hotsell_swiprefresh)
    SwipeRefreshLayout swipeRefreshLayout;
    /**
     * 商品列表
     */
    HotSellAdapter hotSellAdapter;
    List<TwitterGoods> goods = new ArrayList<TwitterGoods>();
    private int page = 0;

    @Override
    protected void afterInject() {
        super.afterInject();
        httpHelper.setCompleteLinstener(this);
    }

    @Override
    protected void init() {
        initSwipeRefreshLayout();
        initView();
    }

    private void initView() {
        hotSellAdapter = new HotSellAdapter(goods, activity);
        staggeredGridView.setAdapter(hotSellAdapter);
        staggeredGridView.setOnScrollListener(new PauseOnScrollListener(ImageLoader.getInstance(), false, true));

    }

    private void initSwipeRefreshLayout() {
        swipeRefreshLayout.setMode(SwipeRefreshLayout.Mode.BOTH);
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setOnLoadListener(this);
        swipeRefreshLayout.setLoadNoFull(false);
        swipeRefreshLayout.setColor(R.color.blue, R.color.green, R.color.yellow, R.color.red);
    }

    /**
     * @return
     */
    private RequestParams getParams() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("offset", String.valueOf(page));
        params.addBodyParameter("limit", "30");
        params.addBodyParameter("imei", "000000000000000");
        params.addBodyParameter("mac", "08%3A00%3A27%3A51%3A2e%3Aaa");
        params.addBodyParameter("qudaoid", "11601");
        params.addBodyParameter("access_token", "d15836232a41bb069709260e2e6d6a20");
        return params;
    }

    @Override
    protected void connect() {
        httpHelper.postData(AppConstants.HOME_POPULAR_POST_URL, getParams(), new TypeReference<AppConn<TwitterGoods>>() {
        });
    }

    /**
     * @param data
     * @param <T>
     */
    @Override
    public <T> void onCompleteLinstener(List<T> data) {
        if (page == 1 && !goods.isEmpty()) {
            goods.clear();
        }
        swipeRefreshLayout.setLoading(false);
        swipeRefreshLayout.setRefreshing(false);
        goods.addAll((List<TwitterGoods>) data);
        hotSellAdapter.notifyDataSetChanged();
    }

    @Override
    public void onRefresh() {
        page = 1;
        connect();

    }

    @Override
    public void onLoad() {
        page++;
        connect();
    }

    @ItemClick(R.id.hotsell_stagger)
    public void onItemClick(int position) {
        Toast.makeText(activity, "----->" + position, Toast.LENGTH_SHORT).show();
    }
}
